package com.yj.web.service.impl;

import com.yj.web.dao.mapper.*;
import com.yj.web.dao.po.admin.Article;
import com.yj.web.dao.po.admin.Banner;
import com.yj.web.dao.po.admin.CustomerService;
import com.yj.web.dao.po.admin.HelpDoc;
import com.yj.web.help.PoUtil;
import com.yj.web.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/4.
 */
@Service("operationService")
public class OperationServiceImpl implements OperationService {

    private static final String PREFIX_SAVE_URL = "http://admin.yinuochoukuan.com";
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private HelpDocMapper helpDocMapper;
    @Autowired
    private CustomerServiceMapper customerServiceMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public List<Banner> listBanner(String title, Integer type, Integer status, int page, int size) {
        int offset = (page - 1) * size;
        List<Banner> banners = bannerMapper.listBanner(title, type, status, offset, size);
        if (null != banners && banners.size() > 0) {
            for (Banner banner : banners) {
                String save_url = banner.getSave_url();
                save_url = PREFIX_SAVE_URL + save_url;
                banner.setSave_url(save_url);
            }
        }
        return banners;
    }

    @Override
    public int countBanner(String title, Integer type, Integer status) {
        int num = bannerMapper.countBanner(title, type, status);
        return num;
    }

    @Override
    public int deleteBannerById(Integer id) {
        int num = bannerMapper.deleteBannerById(id);
        return num;
    }

    @Override
    public long insertBanner(Banner banner) {
        if (null == banner.getPosition()) {
            int maxPosition = bannerMapper.getMaxPosition(banner.getType());
            banner.setPosition(++maxPosition);
        }
        return bannerMapper.insertBanner(banner);
    }

    @Override
    public int generatePosition(int type) {
        int maxPosition = bannerMapper.getMaxPosition(type);
        return ++maxPosition;
    }

    @Override
    public boolean updateBanner(Banner banner) {
        Banner bannerOriginal = bannerMapper.findBannerById(banner.getId());
        if(bannerOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(banner, bannerOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return bannerMapper.updateBanner(bannerOriginal);
    }

    @Override
    public Banner getBannerById(Integer id) {
        return bannerMapper.findBannerById(id);
    }

    @Override
    public boolean updateBannerStatus(int  id, int  status) {
        return bannerMapper.updateBannerStatus(id, status);
    }

    //article
    @Override
    public List<Article> listArticle(String title, Integer status, int page, int size) {
        int offset = (page - 1) * size;
        List<Article> articles = articleMapper.listArticle(title, status, offset, size);
        return articles;
    }

    @Override
    public int countArticle(String title, Integer status) {
        int num = articleMapper.countArticle(title, status);
        return num;
    }

    @Override
    public int deleteArticleById(int id) {
        int num = articleMapper.deleteArticleById(id);
        return num;
    }

    @Override
    public long insertArticle(Article article) {
        return articleMapper.insertArticle(article);
    }

    @Override
    public boolean updateArticle(Article article) {
        Article articleOriginal = articleMapper.findArticleById(article.getId());
        if(articleOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(article, articleOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return articleMapper.updateArticle(articleOriginal);
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleMapper.findArticleById(id);
    }

    @Override
    public boolean updateArticleStatus(int  id, int  status) {
        return articleMapper.updateArticleStatus(id, status);
    }

    //helpDoc
    @Override
    public List<HelpDoc> listHelpDoc(int page, int size) {
        int offset = (page - 1) * size;
        List<HelpDoc> docs = helpDocMapper.listHelpDoc(offset, size);
        return docs;
    }

    @Override
    public int countHelpDoc() {
        int num = helpDocMapper.countHelpDoc();
        return num;
    }

    @Override
    public int deleteHelpDocById(int id) {
        int num = helpDocMapper.deleteHelpDocById(id);
        return num;
    }

    @Override
    public long insertHelpDoc(HelpDoc doc) {
        return helpDocMapper.insertHelpDoc(doc);
    }

    @Override
    public boolean updateHelpDoc(HelpDoc doc) {
        HelpDoc docOriginal = helpDocMapper.findHelpDocById(doc.getId());
        if(docOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(doc, docOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return helpDocMapper.updateHelpDoc(docOriginal);
    }

    @Override
    public HelpDoc getHelpDocById(Integer id) {
        return helpDocMapper.findHelpDocById(id);
    }

    @Override
    public boolean updateHelpDocStatus(int  id, int  status) {
        return helpDocMapper.updateHelpDocStatus(id, status);
    }

    //客服
    @Override
    public List<CustomerService> listCustomerService(int page, int size) {
        int offset = (page - 1) * size;
        List<CustomerService> services = customerServiceMapper.listCustomerService(offset, size);
        if (null != services && 0 != services.size()) {
            for (CustomerService service : services) {
                String nickname = adminUserMapper.getNicknameById(service.getCreate_id());
                service.setNickname(nickname);
                if (null != service.getProcessor_id()) {
                    String realname = employeeMapper.getNameById(service.getProcessor_id());
                    service.setProcessorName(realname);
                }
            }
        }
        return services;
    }

    @Override
    public int countCustomerService() {
        int num = customerServiceMapper.countCustomerService();
        return num;
    }

    @Override
    public CustomerService findCustomerServiceById(String id) {
        CustomerService service = customerServiceMapper.findCustomerServiceById(id);
        return service;
    }

    @Override
    public CustomerService findCustomerServiceByIdFinished(String id) {
        CustomerService service = customerServiceMapper.findCustomerServiceByIdFinished(id);
        return service;
    }

    @Override
    public boolean updateCustomerService(CustomerService service) {
        CustomerService serviceOriginal = customerServiceMapper.findCustomerServiceById(service.getId());
        if(serviceOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(service, serviceOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return customerServiceMapper.updateCustomerService(serviceOriginal);
    }
}
